package net.icontents.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import net.icontents.Constants;
import net.icontents.beans.Article;
import net.icontents.beans.component.ArticleComponent;
import net.icontents.beans.component.ArticleListComponent;
import net.icontents.dao.ArticleComponentDao;
import net.icontents.dao.ArticleDao;
import net.icontents.service.ArticleService;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

@Service
public class ArticleServiceImpl extends BaseServiceImpl<Article> implements ArticleService {

	@Resource
	private ArticleDao articleDao;
	@Resource
	private ArticleComponentDao articleComponentDao;
	
	@Override
	public void setBaseDaoHandlewired() {
		setBaseDao(articleDao);
	}

	@Override
	public int insertArticle(Article article) {
		if(article==null){
			return Constants.ERROR;
		}
		
		int articleId = insert(article);
		article.setId(articleId);
		List<ArticleComponent> componentList = null;
		
		//header
		componentList = article.getHeaderComponentList();
		insertComponentList(componentList,article);
		//left
		componentList = article.getLeftComponentList();
		insertComponentList(componentList,article);
		//right
		componentList = article.getRightComponentList();
		insertComponentList(componentList,article);
		//footer
		componentList = article.getFooterComponentList();
		insertComponentList(componentList,article);
		//body
		componentList = article.getBodyComponentList();
		insertComponentList(componentList,article);
		
		return articleId;
	}
	
	public Article getArticleById(int articleId){
		return getArticleById(articleId,false);
	}
	
	public Article getArticleById(int articleId,boolean hasExtInfo){
		Article article = articleDao.getObjectById(articleId);
		
		//articleComponentDao
		if(article!=null){
			article.setHeaderComponentList(processComponentList(articleComponentDao.selectByArticleId(articleId, ArticleComponent.POSITION_HEADER),hasExtInfo));
			article.setLeftComponentList(processComponentList(articleComponentDao.selectByArticleId(articleId, ArticleComponent.POSITION_LEFT),hasExtInfo));
			article.setRightComponentList(processComponentList(articleComponentDao.selectByArticleId(articleId, ArticleComponent.POSITION_RIGHT),hasExtInfo));
			article.setFooterComponentList(processComponentList(articleComponentDao.selectByArticleId(articleId, ArticleComponent.POSITION_FOOTER),hasExtInfo));
			article.setBodyComponentList(processComponentList(articleComponentDao.selectByArticleId(articleId, ArticleComponent.POSITION_BODY),hasExtInfo));
		}
		
		return article;
	}
	private void insertComponentList(List<ArticleComponent> componentList,Article article){
		for(ArticleComponent articleComponent:componentList){
			articleComponent.setArticleId(article.getId());
			articleComponentDao.insert(articleComponent);
		}
	}

	@Override
	public void updateArticle(Article article) {
		articleDao.update(article);
		
		articleComponentDao.deleteByArticleId(article.getId());
		
		List<ArticleComponent> componentList = null;
		
		//header
		componentList = article.getHeaderComponentList();
		insertComponentList(componentList,article);
		//left
		componentList = article.getLeftComponentList();
		insertComponentList(componentList,article);
		//right
		componentList = article.getRightComponentList();
		insertComponentList(componentList,article);
		//footer
		componentList = article.getFooterComponentList();
		insertComponentList(componentList,article);
		//body
		componentList = article.getBodyComponentList();
		insertComponentList(componentList,article);
	}

	private List<ArticleComponent> processComponentList(List<ArticleComponent> componentList,boolean hasExtInfo){
		if(componentList==null){
			return componentList;
		}
		
		List<ArticleComponent> componentListTarget = new ArrayList<ArticleComponent>();
		
		for(ArticleComponent component:componentList){
			componentListTarget.add(processComponent(component,hasExtInfo));
		}
		
		return componentListTarget;
	}

	private ArticleComponent processComponent(ArticleComponent articleComponent,boolean hasExtInfo) {
		if(articleComponent==null){
			return null;
		}
		ArticleComponent newArticleComponent = null;
				
		//System.out.println("=================================>"+articleComponent);
		try {
            newArticleComponent = (ArticleComponent)(Class.forName(ArticleComponent.class.getPackage().getName()+"." + articleComponent.getComponentType()).getConstructor(new Class[]{ArticleComponent.class}).newInstance(articleComponent));
        } catch (Exception e1) {
            newArticleComponent = articleComponent;
        }
        
        //System.out.println("--------------------------------->"+newArticleComponent);
        if(!hasExtInfo)return newArticleComponent;
            
		if(ArticleListComponent.class.getSimpleName().equalsIgnoreCase(newArticleComponent.getComponentType())){
			ArticleListComponent articleComponentTarget = (ArticleListComponent)(newArticleComponent);
			String content = newArticleComponent.getContent();
			
			if(StringUtils.isBlank(content)){
				return articleComponentTarget;
			}
			
			String[] articleIdStr = content.split(",");
			
			List<Article> articleList = new ArrayList<Article>();
			
			for(String idStr:articleIdStr){
				try{
					articleList.add(getObjectById(Integer.parseInt(idStr)));
				}catch(Exception e){
					e.printStackTrace();
				}
			}
			articleComponentTarget.setArticleList(articleList);
			
			return articleComponentTarget;
		}
		
		return newArticleComponent;
	}
	
	@Override
	public ArticleComponent getArticleComponentById(int articleId,int sequence,int position){
		return articleComponentDao.getArticleComponentById(articleId, sequence, position);
	}
}
